home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_vga.arc / VGA71-L2.ASM < prev   
Assembly Source File  |  1988-10-16  |  7KB  |  264 lines

  1. ;
  2. ; *** Listing 2 ***
  3. ;
  4. ; Program to demonstrate the two pages available in 320x400
  5. ; 256-color modes on a VGA.  Draws diagonal color bars in all
  6. ; 256 colors in page 0, then does the same in page 1 (but with
  7. ; the bars tilted the other way), and finally draws vertical
  8. ; color bars in page 0.
  9. ;
  10. VGA_SEGMENT    equ    0a000h
  11. SC_INDEX    equ    3c4h    ;Sequence Controller Index register
  12. GC_INDEX    equ    3ceh    ;Graphics Controller Index register
  13. CRTC_INDEX    equ    3d4h    ;CRT Controller Index register
  14. MAP_MASK    equ    2    ;Map Mask register index in SC
  15. MEMORY_MODE    equ    4    ;Memory Mode register index in SC
  16. MAX_SCAN_LINE    equ    9    ;Maximum Scan Line reg index in CRTC
  17. START_ADDRESS_HIGH equ    0ch    ;Start Address High reg index in CRTC
  18. UNDERLINE    equ    14h    ;Underline Location reg index in CRTC
  19. MODE_CONTROL    equ    17h    ;Mode Control register index in CRTC
  20. GRAPHICS_MODE    equ    5    ;Graphics Mode register index in GC
  21. MISCELLANEOUS    equ    6    ;Miscellaneous register index in GC
  22. SCREEN_WIDTH    equ    320    ;# of pixels across screen
  23. SCREEN_HEIGHT    equ    400    ;# of scan lines on screen
  24. WORD_OUTS_OK    equ    1    ;set to 0 to assemble for
  25.                 ; computers that can't handle
  26.                 ; word outs to indexed VGA registers
  27. ;
  28. stack    segment para stack 'STACK'
  29.     db    512 dup (?)
  30. stack    ends
  31. ;
  32. ; Macro to output a word value to a port.
  33. ;
  34. OUT_WORD    macro
  35. if WORD_OUTS_OK
  36.     out    dx,ax
  37. else
  38.     out    dx,al
  39.     inc    dx
  40.     xchg    ah,al
  41.     out    dx,al
  42.     dec    dx
  43.     xchg    ah,al
  44. endif
  45.     endm
  46. ;
  47. ; Macro to output a constant value to an indexed VGA register.
  48. ;
  49. CONSTANT_TO_INDEXED_REGISTER    macro    ADDRESS, INDEX, VALUE
  50.     mov    dx,ADDRESS
  51.     mov    ax,(VALUE shl 8) + INDEX
  52.     OUT_WORD
  53.     endm
  54. ;
  55. Code    segment
  56.     assume    cs:Code
  57. Start    proc    near
  58. ;
  59. ; Set 320x400 256-color mode.
  60. ;
  61.     call    Set320By400Mode
  62. ;
  63. ; We're in 320x400 256-color mode, with page 0 displayed.
  64. ; Let's fill page 0 with color bars slanting down and to the right.
  65. ;
  66.     sub    di,di        ;page 0 starts at address 0
  67.     mov    bl,1        ;make color bars slant down and
  68.                 ; to the right
  69.     call    ColorBarsUp    ;draw the color bars
  70. ;
  71. ; Now do the same for page 1, but with the color bars
  72. ; tilting the other way.
  73. ;
  74.     mov    di,8000h    ;page 1 starts at address 8000h
  75.     mov    bl,-1        ;make color bars slant down and
  76.                 ; to the left
  77.     call    ColorBarsUp    ;draw the color bars
  78. ;
  79. ; Wait for a key and flip to page 1 when one is pressed.
  80. ;
  81.     call    GetNextKey
  82.     CONSTANT_TO_INDEXED_REGISTER CRTC_INDEX,START_ADDRESS_HIGH,80h
  83.                 ;set the Start Address High register
  84.                 ; to 80h, for a start address of 8000h
  85. ;
  86. ; Draw vertical bars in page 0 while page 1 is displayed.
  87. ;
  88.     sub    di,di        ;page 0 starts at address 0
  89.     sub    bl,bl        ;make color bars vertical
  90.     call    ColorBarsUp    ;draw the color bars
  91. ;
  92. ; Wait for another key and flip back to page 0 when one is pressed.
  93. ;
  94.     call    GetNextKey
  95.     CONSTANT_TO_INDEXED_REGISTER CRTC_INDEX,START_ADDRESS_HIGH,00h
  96.                 ;set the Start Address High register
  97.                 ; to 00h, for a start address of 0000h
  98. ;
  99. ; Wait for yet another key and return to text mode and end when
  100. ; one is pressed.
  101. ;
  102.     call    GetNextKey
  103.     mov    ax,0003h
  104.     int    10h    ;text mode
  105.     mov    ah,4ch
  106.     int    21h    ;done
  107. ;
  108. Start    endp
  109. ;
  110. ; Sets up 320x400 256-color modes.
  111. ;
  112. ; Input: none
  113. ;
  114. ; Output: none
  115. ;
  116. Set320By400Mode    proc    near
  117. ;
  118. ; First, go to normal 320x200 256-color mode, which is really a
  119. ; 320x400 256-color mode with each line scanned twice.
  120. ;
  121.     mov    ax,0013h  ;AH = 0 means mode set, AL = 13h selects
  122.             ; 256-color graphics mode
  123.     int    10h    ;BIOS video interrupt
  124. ;
  125. ; Change CPU addressing of video memory to linear (not odd/even,
  126. ; chain, or chain 4), to allow us to access all 256K of display
  127. ; memory. When this is done, VGA memory will look just like memory
  128. ; in modes 10h and 12h, except that each byte of display memory will
  129. ; control one 256-color pixel, with 4 adjacent pixels at any given
  130. ; address, one pixel per plane.
  131. ;
  132.     mov    dx,SC_INDEX
  133.     mov    al,MEMORY_MODE
  134.     out    dx,al
  135.     inc    dx
  136.     in    al,dx
  137.     and    al,not 08h    ;turn off chain 4
  138.     or    al,04h        ;turn off odd/even
  139.     out    dx,al
  140.     mov    dx,GC_INDEX
  141.     mov    al,GRAPHICS_MODE
  142.     out    dx,al
  143.     inc    dx
  144.     in    al,dx
  145.     and    al,not 10h    ;turn off odd/even
  146.     out    dx,al
  147.     dec    dx
  148.     mov    al,MISCELLANEOUS
  149.     out    dx,al
  150.     inc    dx
  151.     in    al,dx
  152.     and    al,not 02h    ;turn off chain
  153.     out    dx,al
  154. ;
  155. ; Now clear the whole screen, since the mode 13h mode set only
  156. ; cleared 64K out of the 256K of display memory. Do this before
  157. ; we switch the CRTC out of mode 13h, so we don't see garbage
  158. ; on the screen when we make the switch.
  159. ;
  160.     CONSTANT_TO_INDEXED_REGISTER SC_INDEX,MAP_MASK,0fh
  161.                 ;enable writes to all planes, so
  162.                 ; we can clear 4 pixels at a time
  163.     mov    ax,VGA_SEGMENT
  164.     mov    es,ax
  165.     sub    di,di
  166.     mov    ax,di
  167.     mov    cx,8000h    ;# of words in 64K
  168.     cld
  169.     rep    stosw        ;clear all of display memory
  170. ;
  171. ; Tweak the mode to 320x400 256-color mode by not scanning each
  172. ; line twice.
  173. ;
  174.     mov    dx,CRTC_INDEX
  175.     mov    al,MAX_SCAN_LINE
  176.     out    dx,al
  177.     inc    dx
  178.     in    al,dx
  179.     and    al,not 1fh    ;set maximum scan line = 0
  180.     out    dx,al
  181.     dec    dx
  182. ;
  183. ; Change CRTC scanning from doubleword mode to byte mode, allowing
  184. ; the CRTC to scan more than 64K of video data.
  185. ;
  186.     mov    al,UNDERLINE
  187.     out    dx,al
  188.     inc    dx
  189.     in    al,dx
  190.     and    al,not 40h    ;turn off doubleword
  191.     out    dx,al
  192.     dec    dx
  193.     mov    al,MODE_CONTROL
  194.     out    dx,al
  195.     inc    dx
  196.     in    al,dx
  197.     or    al,40h    ;turn on the byte mode bit, so memory is
  198.             ; scanned for video data in a purely
  199.             ; linear way, just as in modes 10h and 12h
  200.     out    dx,al
  201.     ret
  202. Set320By400Mode    endp
  203. ;
  204. ; Draws a full screen of slanting color bars in the specified page.
  205. ;
  206. ; Input:
  207. ;    DI = page start address
  208. ;    DL = 1 to make the bars slant down and to the right, -1 to
  209. ;        make them slant down and to the left, 0 to make
  210. ;        them vertical.
  211. ;
  212. ColorBarsUp    proc    near
  213.     mov    ax,VGA_SEGMENT
  214.     mov    es,ax    ;point to display memory
  215.     sub    bh,bh    ;start with color 0
  216.     mov    si,SCREEN_HEIGHT ;# of rows to do
  217.     mov    dx,SC_INDEX
  218.     mov    al,MAP_MASK
  219.     out    dx,al    ;point the SC Index reg to the Map Mask reg
  220.     inc    dx    ;point DX to the SC Data register
  221. RowLoop:
  222.     mov    cx,SCREEN_WIDTH/4
  223.             ;there are 4 pixels at each address, so
  224.             ; each 320-pixel row is 80 bytes wide
  225.             ; in each plane
  226.     push    bx    ;save the row-start color
  227. ColumnLoop:
  228. MAP_SELECT = 1
  229.     rept    4    ;do all 4 pixels at this address with
  230.             ; in-line code
  231.     mov    al,MAP_SELECT
  232.     out    dx,al    ;select planes 0, 1, 2, and 3 in turn
  233.     mov    es:[di],bh ;write this plane's pixel
  234.     inc    bh    ;set the color for the next pixel
  235. MAP_SELECT = MAP_SELECT shl 1
  236.     endm
  237.     inc    di    ;point to the address containing the next
  238.             ; 4 pixels
  239.     loop    ColumnLoop ;do any remaining pixels on this line
  240.     pop    bx    ;get back the row-start color
  241.     add    bh,bl    ;select next row-start color (controls
  242.             ; slanting of color bars)
  243.     dec    si    ;count down lines on the screen
  244.     jnz    RowLoop
  245.     ret
  246. ColorBarsUp    endp
  247. ;
  248. ; Waits for the next key and returns it in AX.
  249. ;
  250. GetNextKey    proc    near
  251. WaitKey:
  252.     mov    ah,1
  253.     int    16h
  254.     jz    WaitKey    ;wait for a key to become available
  255.     sub    ah,ah
  256.     int    16h    ;read the key
  257.     ret
  258. GetNextKey    endp
  259. ;
  260. Code    ends
  261. ;
  262.     end    Start
  263.  
  264.